# -*- coding: utf-8 -*-

from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect

EXCEPT_URLS=[
	reverse('zaloguj'),
	reverse('zawitaj'),
]

class LoginMiddleware(object):
	def process_request(self, request):
		if request.path not in EXCEPT_URLS:
			if request.user.is_authenticated():
				if request.user.is_active:
					pass
				else:
					return HttpResponse(u'Konto nie jest aktywne, skontaktuj się z szefem ;]')
			else:
				if request.COOKIES.has_key('zawitaj'):
					if request.COOKIES['zawitaj']=='1':
						return HttpResponseRedirect(reverse('zaloguj'))
				return HttpResponseRedirect(reverse('zawitaj'))
	
	def process_response(self, request, response):
		""" BRIDGE SPLASH :) """
		if request.method == "POST":
			if request.POST.has_key('pamietaj'):
				if request.POST['pamietaj'] == "True":
					response.set_cookie('zawitaj', value='1', max_age=2592000)
		return response
